package com.yihu.wlyy.service.app.intervene;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.yihu.wlyy.entity.patient.PatientInspectionSuggest;
import com.yihu.wlyy.entity.patient.PatientSelfInspection;
import com.yihu.wlyy.entity.patient.PatientSelfInspectionItem;
import com.yihu.wlyy.repository.PatientInspectionSuggestDao;
import com.yihu.wlyy.repository.PatientSelfInspectionDao;
import com.yihu.wlyy.repository.PatientSelfInspectionItemDao;
import com.yihu.wlyy.service.BaseService;

/**
 * 患者检查、自查建议业务处理类
 * @author George
 *
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class PatientInspectionService extends BaseService {

	@Autowired
	private PatientSelfInspectionDao patientSelfInspectionDao;
	@Autowired
	private PatientSelfInspectionItemDao patientSelfInspectionItemDao;
	@Autowired
	private PatientInspectionSuggestDao patientInspectionSuggestDao;

	/**
	 * 保存患者自查建议
	 * @param inspection 自查对象
	 * @param items 自查明细列表
	 * @return
	 */
	public void save(PatientSelfInspection inspection, List<PatientSelfInspectionItem> items) {
		patientSelfInspectionDao.save(inspection);
		patientSelfInspectionItemDao.save(items);
	}

	/**
	 * 保存患者检查建议
	 * @param pis
	 */
	public PatientInspectionSuggest save(PatientInspectionSuggest pis) {
		return patientInspectionSuggestDao.save(pis);
	}

	/**
	 * 查询指定患者的体测提醒
	 * @param doctor 医生标识
	 * @param patient 患者标识
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<PatientSelfInspection> findPatientByDoctor(String doctor, String patient, long id, int pagesize) {
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);

		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		filters.put("doctor", new SearchFilter("doctor", Operator.EQ, doctor));
		filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<PatientSelfInspection> spec = DynamicSpecifications.bySearchFilter(filters.values(), PatientSelfInspection.class);
		return patientSelfInspectionDao.findAll(spec, pageRequest);
	}

	/**
	 * 查询患者体测提醒
	 * @param patient 患者标识
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<PatientSelfInspection> findSelfExamByPatient(String patient, long id, int pagesize) {
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);

		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<PatientSelfInspection> spec = DynamicSpecifications.bySearchFilter(filters.values(), PatientSelfInspection.class);
		return patientSelfInspectionDao.findAll(spec, pageRequest);
	}

	/**
	 * 查询体检提醒信息
	 * @param inspection 提醒标识
	 * @return
	 */
	public List<PatientSelfInspectionItem> findSelfExamInfo(String inspection) {
		return patientSelfInspectionItemDao.findByInspection(inspection);
	}

	/**
	 * 查询医生为该患者发送的检查建议
	 * @param doctor 医生标识
	 * @param patient 患者标识
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<PatientInspectionSuggest> findPatientSuggestByDoctor(String doctor, String patient, long id, int pagesize) {
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);

		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		filters.put("doctor", new SearchFilter("doctor", Operator.EQ, doctor));
		filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<PatientInspectionSuggest> spec = DynamicSpecifications.bySearchFilter(filters.values(), PatientInspectionSuggest.class);
		return patientInspectionSuggestDao.findAll(spec, pageRequest);
	}
	
	/**
	 * 查患者的检查建议
	 * @param patient 患者标识
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<PatientInspectionSuggest> findSuggestByPatient(String patient, long id, int pagesize) {
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);

		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		filters.put("patient", new SearchFilter("patient", Operator.EQ, patient));
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		// 未作废
		filters.put("del", new SearchFilter("del", Operator.EQ, "1"));
		Specification<PatientInspectionSuggest> spec = DynamicSpecifications.bySearchFilter(filters.values(), PatientInspectionSuggest.class);
		return patientInspectionSuggestDao.findAll(spec, pageRequest);
	}

}
